package com.example.mybatis.demo.mapper;

import com.example.mybatis.demo.model.UserInfo;
import org.apache.catalina.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Li_yizYa
 * Date: 2025/7/14
 * Time: 20:51
 */
@Mapper
public interface UserInfoMapper {
    // 查询所有的用户信息
//    @Select("select id, username, password, age, gender, phone, " +
//            "delete_flag as deleteFlag, create_time as createTime, update_time as updateTime from user_info")
//    @Results(id = "BaseMap", value = {
//            @Result(column = "delete_flag", property = "deleteFlag"),
//            @Result(column = "create_time", property = "createTime"),
//            @Result(column = "update_time", property = "updateTime")
//    })
    @Select("select id, username, password, age, gender, phone, delete_flag, create_time, update_time from user_info")
    List<UserInfo> selectAll();

//    @Select("select * from user_info")
//    UserInfo selectAll2();

//    @ResultMap("BaseMap")
    @Select("select id, username, password, age, gender, phone, delete_flag, " +
            "create_time, update_time from user_info where id= #{id}")
    UserInfo selectUserById(Integer id);

    // 非唯一主键查询，建议返回结果使用 List
//    @Select("select * from user_info where age=#{age} and gender=#{gender}") // 正确的写法
//    @Select("select * from user_info where age=#{age1} and gender=#{gender}") // 参数绑定异常
//    @Select("select * from user_info where age=#{param2} and gender=#{param1}") // 可以正确执行，不推荐
    @Select("select * from user_info where age=#{age1} and gender=#{gender1}") // 参数重命名
    List<UserInfo> selectUserByAgeAndGender(@Param("gender1") Integer gender, @Param("age1") Integer age);

    // 传递对象
    @Insert("insert into user_info (username, `password`, age, gender) " +
            "values (#{username}, #{password}, #{age}, #{gender})")
    Integer insertUser(UserInfo userInfo);

    // 传递对象
    @Options(useGeneratedKeys = true, keyProperty = "id")
    @Insert("insert into user_info (username, `password`, age, gender) " +
            "values (#{userInfo.username}, #{userInfo.password}, #{userInfo.age}, #{userInfo.gender})")
    Integer insertUserByParam(@Param("userInfo") UserInfo userInfo);

    // 删除数据
    @Delete("delete from user_info where id=#{id}")
    Integer delete(Integer id);

    // 修改数据
    @Update("update user_info set password=#{password} where id=#{id}")
    Integer update(Integer id, String password);

    // $ 和 # 的区别
    @Select("select * from user_info where id=#{id}")
    List<UserInfo> selectUserById1(Integer id);

    @Select("select * from user_info where username = '${name}' and password = '${password}'")
    List<UserInfo> selectUserByName(String name, String password);

    // 排序功能，只能使用 $
    @Select("select * from user_info order by id ${order}")
    List<UserInfo> selectUserByOrder(String order);

//    @Select("select * from user_info where username like '%${name}%'")
    @Select("select * from user_info where username like CONCAT('%', #{name}, '%')")
    List<UserInfo> selectUserByLike(String name);
}
